package com.example.config;


import com.example.user.UserPhone.SendCode;
import com.example.user.UserPhone.ValidateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping("Verify")
public class VerifyCode {

    String code;
    @Autowired
    private StringRedisTemplate srt;

    @GetMapping("getphone")
    public String GetPhone(String Code) {
        System.out.println(Code);
        if (srt.opsForValue().get(Code) == null) {
            Integer s = ValidateCode.generateValidateCode(6);

            SendCode.sendCode(Code, s.toString());
            srt.opsForValue().set(Code, s.toString(), 60, TimeUnit.SECONDS);
            return "发送成功";
        }
        return "发送频率过快！！";
    }

    @PostMapping("/verityCode")
    public String Verity(@RequestBody Map<String, String> Code) {
        String one = srt.opsForValue().get(Code.get("phone"));
        String two = Code.get("Codes");
        System.out.println(one + two);
        if (null == one) {
            return "验证失败啦";
        } else if (one.equals(two)) {
            return "验证成功";
        }
        return "";
    }

}
